package com.sgb.goods.view.activity.inquiry;

import android.content.Context;
import android.content.Intent;
import android.view.inputmethod.EditorInfo;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.KeyboardUtils;
import com.google.android.material.tabs.TabLayoutMediator;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityMyInquiryBinding;
import com.sgb.goods.entity.GoodsItemEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.utils.module.GoodsModule;
import com.sgb.goods.view.fragment.inquiry.MyInquiryFragment;
import com.sgb.goods.viewmodel.inquiry.MyInquiryModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

/**
 * 描述：个人中心--我的询价
 *
 * @author qiangHeNooo
 * <p>
 * 时间：2021.10.28
 */
@Route(path = Constants.ARouter.PERSONAL_MY_INQUIRY)
public class MyInquiryActivity extends BaseActivity<GoodsActivityMyInquiryBinding, MyInquiryModel> {

    /**
     * 待报价
     */
    public static final int TAB_WAITING = 1;
    /**
     * 已报价
     */
    public static final int TAB_QUOTED = 2;
    /**
     * 已失效
     */
    public static final int TAB_EXPIRED = 4;

    private final String[] titleName = {"待报价", "已报价", "已失效"};
    private ArrayList<Fragment> fragmentArrayList;

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_my_inquiry;
    }

    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, MyInquiryActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        mDataBinding.goodsSearchPriceCheck.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mDataBinding.goodsSearchPriceCheck.setOnEditorActionListener((v, keyCode, event) -> {
            if (keyCode == EditorInfo.IME_ACTION_SEARCH) {
                mDataBinding.goodsSearchPriceCheck.setFocusableInTouchMode(false);
                KeyboardUtils.hideSoftInput(mDataBinding.goodsSearchPriceCheck);
                mViewModel.searchContent.setValue(v.getText().toString().trim());
                return true;
            }
            return false;
        });
        if (fragmentArrayList == null) {
            fragmentArrayList = new ArrayList<>();
        }
        fragmentArrayList.add(MyInquiryFragment.newInstance(TAB_WAITING));
        fragmentArrayList.add(MyInquiryFragment.newInstance(TAB_QUOTED));
        fragmentArrayList.add(MyInquiryFragment.newInstance(TAB_EXPIRED));
        mDataBinding.goodsViewPager.setAdapter(new FragmentStateAdapter(MyInquiryActivity.this) {
            @NotNull
            @Override
            public Fragment createFragment(int position) {
                return fragmentArrayList.get(position);
            }

            @Override
            public int getItemCount() {
                return fragmentArrayList.size();
            }
        });
        TabLayoutMediator layoutMediator = new TabLayoutMediator(mDataBinding.goodTabLayout, mDataBinding.goodsViewPager, (tab, position) -> tab.setText(titleName[position]));
        layoutMediator.attach();
        mDataBinding.goodsViewPager.setOffscreenPageLimit(titleName.length);
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(MyInquiryModel.class, BR.viewModel);
    }

    @Override
    protected void initListener() {
        mDataBinding.goodsBackView.setOnClickListener(v -> finish());
        mDataBinding.goodsPublishPriceCheck.setOnClickListener(v -> {
            InquiryPublishingActivity.start(MyInquiryActivity.this,null);
//            Object object = GoodsModule.getInstance().call2("getInquiryPublishPermission");
//            if (object != null) {
//                int type = (int) object;
//                if (type == -1) {
//                    ARouter.getInstance().build(Constants.ARouter.AR_INQUIRY_NEW_PURCHASES)
//                            .withBoolean("GOOD_EDIT", false)
//                            .withBoolean("GOOD_ADD", false)
//                            .withSerializable("GOOD_ENTITY", new GoodsItemEntity())
//                            .navigation();
//                } else {
//                    GoodsModule.getInstance().call1("showInquiryPublishDialog", type);
//                }
//            }
        });
//        <!--此处是新添加的删除搜索内容按钮，不确定是否需要新增,如需新增，将注释代码取消即可-->
//        mDataBinding.goodsIvSearchDelete.setOnClickListener(v -> mDataBinding.goodsSearchPriceCheck.setText(""));
//        mDataBinding.goodsSearchPriceCheck.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if (TextUtils.isEmpty(s)) {
//                    mDataBinding.goodsIvSearchDelete.setVisibility(View.GONE);
//                } else {
//                    mDataBinding.goodsIvSearchDelete.setVisibility(View.VISIBLE);
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//            }
//        });
    }

}